<div id="basket">
<?php 
//ini_set('display_errors',1); 
//error_reporting(E_ALL);

	//Main Basket class integration - MOVED TO HEADER.INC
	//-----------------------------
//	Basket::UnsetVars();
//	$additem						= false;
//	$action							= Basket::GetAction($_REQUEST);
//	if($action) { Basket::Process(); }
	//------------------------------------------------------------

	$basket_sql						= Basket::SQL();
	$array							= Basket::CheckoutArray();
	$basket_db_resource_items		= DB::query($basket_sql['accessory']);
//echo "<p>action: ". $action . "</p>";
//echo "<p>basket_sql: ". print_r($basket_sql) . "</p>";
//echo "<p>array: ". print_r($array) . "</p>";
//echo "<p>basket_db_resource_items: ". print_r($basket_db_resource_items) . "</p>";
	//-----------------------------
	//-----------------------------
	
	//set image checker backup (currently uses $loc to turn relative into absolute)
   	$ImageChecker->setBackupImage($loc . "images/staticbackups/notfound146x106.png");	

   	//set main basket variables
	//-----------------------------
	$basket_summary_array				= Basket::Summary();
//echo "<p>basket_summary_array: ". print_r($basket_summary_array) . "</p>";
	$basket_total_num_items 			= $basket_summary_array['quantity'];
	$basket_total_num_text 				= " Artikel";
	if ($basket_total_num_items > 1) $basket_total_num_text. "s";		//add an 's' to the text if more than one item.
	$basket_items_price_total			= 0;		//might be calulated here when going through the items
	$basket_items_price_saving			= 0;		//same as the total, might be calculated through items loop
	$basket_items_max_quantity			= 5;
	//-----------------------------
	//function defaults
	//-----------------------------
	$basket_function_desc_separator		= "****";	//CostBiters seperation string in ExtendedDesc
	$basket_function_desc_limit			= 180;	//average search index to use to show an inital amount of basket text
	//-----------------------------
	
?>
	<div id="basket_header_summary">
		<div id="basket_header_summaryinner">
			<div id="basket_header_summarytitle"><?=Language::Get('basket',0)?></div>
			<div id="basket_header_summaryinfo">Sie haben <strong><?php echo $basket_total_num_items . $basket_total_num_text; ?> in Ihrem Warenkorb.</strong> Siehe unten:</div>
			<div id="basket_header_summarydelivery"><?=Language::Get('basket',1)?></div>
		</div>
	</div>
    <div class="basket_separator"></div>
    <table id="basket_items" cellpadding="0" cellspacing="0" border="0">
        <tr class="basket_items_headerrow">
            <th></th>
            <th class="left"><?=Language::Get('basket',2)?></th>
            <th class="left"><?=Language::Get('basket',3)?></th>
            <th><?=Language::Get('basket',4)?></th>
        </tr>
<?php 
	        //cycle through each basket item
	        //-----------------------------
			$basket_current_item_index = 0;
			while ($basket_current_item = DB::fetch($basket_db_resource_items)) {
//print_r($basket_current_item);
				$basket_current_item_index			+= 1;
				$basket_current_item_cartid			= $basket_current_item['CartID'];
	        	//grab info here that is needed in the table columns
	        	$basket_current_item_title			= $basket_current_item['AccessoryTitle'];
	        	//note the desc text includes the links and spans for more info
	        	$basket_current_item_desc			= basket_item_clean_description($basket_current_item['AccessoryDescription'], $basket_current_item_title, $basket_current_item_index);
	        	$basket_current_item_stockcode		= $basket_current_item['AccessoryStockCode'];
				$basket_current_item_imageelem		= $ImageChecker->getImg("/web3g/img/costbiter/". $basket_current_item_stockcode .".jpg", "146", "106", $basket_current_item_title, "", "");
				$basket_current_item_pricerrp		= $basket_current_item['AccessoryRRP'];
				$basket_current_item_pricecurrent	= $basket_current_item['AccessoryPrice'];
				if ($basket_current_item_pricerrp > 0 && $basket_current_item_pricecurrent > 0) {	//if no offer, then set the saving to 0
					$basket_current_item_pricesaving	= round(100 * (1 - ($basket_current_item_pricecurrent / $basket_current_item_pricerrp)));
				} else { $basket_current_item_pricesaving = 0; }
				$basket_current_item_quantity		= $basket_current_item['Quantity'];
				if ($basket_current_item_pricecurrent > 0) {		//only the RRP may be set
					$basket_items_price_total		+= $basket_current_item_pricecurrent * $basket_current_item_quantity;
					$basket_items_price_saving		+= ($basket_current_item_pricerrp - $basket_current_item_pricecurrent) * $basket_current_item_quantity;
				}
				else {
					$basket_items_price_total		+= $basket_current_item_pricerrp * $basket_current_item_quantity;
				}
				//-----------------------------  
?>
            <tr>
                <td class="item_number"><?php echo $basket_current_item_index; ?>.</td>
                <td class="item_image">
                	<?php echo $basket_current_item_imageelem; ?>
                </td>
                <td class="item_description">
                	<h3><?php echo $basket_current_item_title; ?></h3>
                	<p><?php echo $basket_current_item_desc; ?></p>
                	<form id="basket_form_itemremove_<?php echo $basket_current_item_index; ?>" action="/basket/" method="post"><input type="hidden" name="esid" value="<?php echo $_SESSION['esid']; ?>" /><input type="hidden" name="remove" value="remove" /><input type="hidden" name="ctid" value="<?php echo $basket_current_item_cartid; ?>" /><a href="#" onclick="parentNode.submit()" class="item_description_remove">Aus dem Warenkorb entfernen</a></form>
                </td>
                <td class="item_price">
                    <?php if ($basket_current_item_pricecurrent > 0) { echo "<span class=\"rrp\">Was &euro;" . sprintf("%1\$.2f", $basket_current_item_pricerrp); } else { echo "<span>&nbsp;"; } ?></span><br />
                    <span class="price">&euro;<?php if ($basket_current_item_pricecurrent > 0) { echo sprintf("%1\$.2f", $basket_current_item_pricecurrent); } else {  echo sprintf("%1\$.2f", $basket_current_item_pricerrp); } ?></span><br />
                    <span class="saving"><?php if ($basket_current_item_pricesaving > 0) { echo "SAVE ".  $basket_current_item_pricesaving . "%"; } else { echo "&nbsp;"; } ?></span><hr width="80%" />
                    <?=Language::Get('basket',5)?>
                    <form id="basket_form_itemquantity_<?php echo $basket_current_item_index; ?>" action="/basket/" method="post">
		                <input type="hidden" name="esid" value="<?php echo $_SESSION['esid']; ?>" />
		                <input type="hidden" name="ctid" value="<?php echo $basket_current_item_cartid; ?>" />
	                    <select name="quantity" onchange="parentNode.submit()">
	                    	<?php 
	                    		for($j = 1; $j <= $basket_items_max_quantity; $j++) {
	                    			if ($j == $basket_current_item_quantity) { echo "<option value=\"$basket_current_item_quantity\" selected>"; }
	                    			else { echo "<option value=\"$j\">"; }
//	                    			echo "<input type=\"hidden\" name=\"number\" value=\"". $j ."\" />";
	                    			echo $j . "</option>\n";
	                    		}
	                    	?>
	                    </select>
                    </form>
                </td>
            </tr>
            <tr class="basket_items_separator"><td colspan="4"></td></tr>
<?php  
	        } 
	        //-----------------------------
?>
        <tr><td colspan="4" class="checkoutseparator"></td></tr>
        <tr>
            <td colspan="4" class="right">
            	<span id="basket_saving_price"><?php if($basket_items_price_saving > 0) Language::Get('basket',6). "&euro;". sprintf("%1\$.2f", $basket_items_price_saving); ?>
            	<img src="/images/spacer.gif" width="40" height="10" />Total:</span>
				<span id="basket_total_price">&euro;<?php echo sprintf("%1\$.2f", $basket_items_price_total); ?></span>
				<img src="/images/spacer.gif" width="20" height="10" />
			</td>
        </tr>
        <tr><td colspan="4" class="checkoutseparator"></td></tr>
        <tr>
            <td colspan="2" class="basket_continueshopping"><a href="<?php echo $loc; ?>"><img src="/images/spacer.gif" class="spritebg continueshop" /></a></td>
            <td colspan="2" class="basket_checkout right">
				<?php if($basket_total_num_items > 0) { ?>
                <form id="basket_form_submit" action="/basket/" method="post">
					<input type="hidden" name="checkout" value="checkout" />
					<input type="image" src="/images/spacer.gif" class="spritebg checkout" />
				</form>
				<?php } ?></td>
        </tr><?php //if($basket_total_num_items > 0) { echo "<img src=\"/images/spacer.gif\" class=\"spritebg checkout\" />"; } ?>
    </table>
    <div class="basket_separator"></div>
</div>
<?php 
	//basket related functions
	function basket_item_clean_description($desc, $title, $index) {
		GLOBAL $basket_function_desc_separator;
		GLOBAL $basket_function_desc_limit;
		
		//first split the description since CostBiter are putting all data (desc,spec,features... etc) in ExtendedDesc and we only need the first section
		$separatorposition = strpos($desc, $basket_function_desc_separator);
		if ($separatorposition !== false) {
			$currentstring = substr($desc, 0, $separatorposition);
			//hack off the 2 page breaks also included at the end of the section string
			$currentstring = substr($currentstring, 0, (strlen($currentstring) - 12));
		}
		else { $currentstring = $desc; }	//no separator, just copy the string info
		
		//now insert a hiding span if applicable
		if (strlen($currentstring) > $basket_function_desc_limit) {
			$splitindex = strpos($currentstring, " ", $basket_function_desc_limit);
			$currentstring = substr($currentstring, 0, $splitindex) . "<span class=\"basket_item_hide\" id=\"basket_item_hide_". $index ."\">" . substr($currentstring, $splitindex, (strlen($currentstring) - $splitindex)) . "</span>";
			$currentstring .= "<a href=\"#\" onclick=\"document.getElementById('basket_item_hide_". $index ."').style.display = 'inline';document.getElementById('basket_item_hidelink_". $index ."').style.display = 'none';\" class=\"item_description_moreinfo\" id=\"basket_item_hidelink_". $index ."\" title=\"Expand the info on $title\">... more info</a>";
		}
		return $currentstring;	
	}

?>